package cn.edu.neusoft.neufooddemo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.CartAdapter;
import cn.edu.neusoft.neufooddemo.bean.OrderInfo;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;

public class MyCartActivity extends BaseActivity implements RetrofitListener<List<OrderInfo>> {

    private RecyclerView mycart_rv;
    private ImageView mycart_return;
    public static TextView cart_sum;
    private CartAdapter adapter;
    private FoodModel foodModel;
    private Button cart_pay;

    protected void initViews() {
        setLayout(R.layout.activity_my_cart);
        mycart_rv = (RecyclerView)findViewById(R.id.mycart_rv);
        mycart_rv.setLayoutManager(new LinearLayoutManager(MyCartActivity.this));
        cart_pay=(Button)findViewById(R.id.cart_pay);
        cart_sum=findViewById(R.id.cart_sum);
        mycart_return = (ImageView)findViewById(R.id.mycart_return);
    }


    void initEvents(){
        mycart_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cart_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<OrderInfo> list=adapter.getData();
               // showToast(list.size()+"个东西");
                if(list.size()==0)
                {
                    showToast("没有选择内容");
                    return;
                }
                String items="";
                for(int i=0;i<list.size();i++)
                {
                    items+=list.get(i).getItem_id();
                    if(i!=list.size()-1)
                        items+=",";
                }
                Intent intent=new Intent(MyCartActivity.this,CartOrderActivity.class);
                intent.putExtra("items",items);
                startActivity(intent);
            }
        });
    }

    void initData(){
        foodModel = new FoodModel();
        foodModel.getMyCartByUser(getUser_id(),this);
    }

    @Override
    public void onSuccess(List<OrderInfo> orderInfos, int flag) {
        System.out.print(orderInfos);
        adapter = new CartAdapter(MyCartActivity.this, orderInfos, R.layout.cardview_cart);
        adapter.setOnItemClickListener(new CartAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                showToast(position+"");

            }
        });
        mycart_rv.setAdapter(adapter);
    }

    @Override
    public void onFail() {
        showToast("网络错误");
    }
}
